package com.apps.helper;

//import com.apps.base.Admin.model.po.Admin;
//import com.apps.base.Member.model.po.Member;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 用ThreadLocal来存储Session,以便实现Session any where
 *
 * @author lingpeng
 * @version v1.0
 * @Since 2019-02-13 11:26
 * 新增request any where
 */
@Slf4j
public class ThreadContextHelper {

    private static ThreadLocal<HttpSession> SessionThreadLocalHolder = new ThreadLocal<HttpSession>();
    private static ThreadLocal<HttpServletRequest> HttpRequestThreadLocalHolder = new ThreadLocal<HttpServletRequest>();
    private static ThreadLocal<HttpServletResponse> HttpResponseThreadLocalHolder = new ThreadLocal<HttpServletResponse>();


    public static void setHttpRequest(HttpServletRequest request) {
        HttpRequestThreadLocalHolder.set(request);
    }

    public static HttpServletRequest getHttpRequest() {
        return HttpRequestThreadLocalHolder.get();
    }

    /**
     * 将每次的请求、响应、Session对象设置到当前线程中，以便随时随地使用
     *
     * @param req
     * @param resp
     */
    public static void setLocal(HttpServletRequest req, HttpServletResponse resp) {
        HttpRequestThreadLocalHolder.set(req);
        SessionThreadLocalHolder.set(req.getSession());
        HttpResponseThreadLocalHolder.set(resp);
    }

    public static void setLocal(HttpServletRequest req, HttpServletResponse resp, HttpSession httpSession) {
        HttpRequestThreadLocalHolder.set(req);
        SessionThreadLocalHolder.set(httpSession);
        HttpResponseThreadLocalHolder.set(resp);
    }

    /**
     * 将每次的请求、响应、Session对象从当前线程移除
     */
    public static void remove() {
        SessionThreadLocalHolder.remove();
        HttpRequestThreadLocalHolder.remove();
        HttpResponseThreadLocalHolder.remove();
    }

    public static void setHttpResponse(HttpServletResponse response) {
        HttpResponseThreadLocalHolder.set(response);
    }

    public static HttpServletResponse getHttpResponse() {
        return HttpResponseThreadLocalHolder.get();
    }

    public static void setSession(HttpSession session) {
        SessionThreadLocalHolder.set(session);
    }


    public static HttpSession getSession() {
        if (SessionThreadLocalHolder.get() == null) {
            HttpServletRequest httpRequest = getHttpRequest();
            SessionThreadLocalHolder.set((httpRequest != null) ? httpRequest.getSession() : null);
        } else {
//			if(logger.isDebugEnabled())
            //System.out.println(" webSessionContext not null and return ...");
        }
        return SessionThreadLocalHolder.get();
    }

    /**
     * 用户登录
     * @param member
     */
//    public static void MemberLogin(Member member)
//    {
//        getSession().setAttribute(MemberConextHelper.CURRENT_MEMBER_KEY,member);
//    }

    /**
     * 用户退出登录
     */
    public static void MemberLoginOut()
    {
        getSession().removeAttribute(MemberConextHelper.CURRENT_MEMBER_KEY);
    }
    /**
     * 管理员登录
     * @param admin
     */
//    public static void AdminLogin(Admin admin)
//    {
//        getSession().setAttribute(MemberConextHelper.CURRENT_ADMIN_KEY,admin);
//    }

    /**
     * 管理员退出登录
     */
    public static void AdminLoginOut()
    {
        getSession().removeAttribute(MemberConextHelper.CURRENT_ADMIN_KEY);
    }
    /**
     * 获取网站的url
     *
     * @return
     */
//    public static String getUrl() {
//
//        String uri = getHttpRequest().getRequestURI().toString();
//        return getHttpRequest().getRequestURL().toString().replaceAll(uri, "");
//    }

}